package com.acme.bankapp.service;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Iterator;



import java.util.logging.Logger;

import com.acme.bankapp.domain.Account;
import com.acme.bankapp.domain.Client;
import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.domain.Gender;
/**
 * BankService class is designed to work with the administrator. All methods of this class duplicate class methods Bank with the changes:
 * 1) Methods public void withdrawClient(Bank bank, String FirstName, String LastName, Gender gender, double x), public void depositeClient(Bank bank, String FirstName, String LastName, Gender gender, double x),
 *  public void printClient(Bank bank, String FirstName, String LastName, Gender gender) pre-seeking client by name in the list of bank customers.
 *   After that call the corresponding method of the class Bank.
 * 2) The method public void addClient(Bank bank, String data) first creates an instance of the class.
 * 3) The methods public Bank readBank () and public void saveBank (Bank bank) are for deserialization and serialization the bank.
 * @author Troitskaya Aanastasia
 *
 */
public class BankService {
    private static final Logger log = Logger. getLogger(BankService.class.getName());
	public void printAllClients(Bank bank) {
		//bank.printAllClients();
	}
	public void printBalance(Bank bank) {
		bank.printBalance();
	}
	public void addClient(Bank bank, String data) {
		@SuppressWarnings("unused")
		String type;
		StringParse parse = new StringParse(data);
		type = parse.getAccountType();
		Client client = new Client(parse.getBalance(), parse.getOverDraft(), parse.getFirstName(), parse.getLastName(), parse.getGender());
		if (client.getAccount() != null) {
			client.getAccount().setClient(client);
			bank.addClient(client);
			saveBank(bank);
		}
	}
	public void withdrawClient(Bank bank, String FirstName, String LastName, Gender gender, double x) {
		bank.withdrawClient(bank.searchClient(FirstName, LastName, gender), x);
		saveBank(bank);
	}
	public void depositeClient(Bank bank, String FirstName, String LastName, Gender gender, double x) {
		bank.depositeClient(bank.searchClient(FirstName, LastName, gender), x);
		saveBank(bank);
	}
	public void printClient(Bank bank, String FirstName, String LastName, Gender gender) {
		bank.printClient(bank.searchClient(FirstName, LastName, gender));
		saveBank(bank);
	}
	public void deleteClient(Bank bank, String FirstName, String LastName, Gender gender) {
		bank.delete(bank.searchClient(FirstName, LastName, gender));
		
	}
	public void saveBank (Bank bank) {
		try {
			FileOutputStream f = new FileOutputStream("bank.ser");
			ObjectOutputStream oos = new ObjectOutputStream(f);
			List<Client> helpList = bank.getList();
			Iterator<Client> iter = helpList.iterator();
			while (iter.hasNext()) {
				Client clientObj = iter.next();
				oos.writeObject(clientObj.getAccount());
				oos.writeObject(clientObj);
			}
			oos.close();
			f.close();

            log.info("Serialization is successful");
		}
		catch (IOException e) {
			log.info("Serialization is not successful");
		}
		
	}
	public void transferBank(Bank bank, ObjectOutputStream out) {
		try {
			List<Client> helpList = bank.getList();
			Iterator<Client> iter = helpList.iterator();
			while (iter.hasNext()) {
				Client clientObj = iter.next();
				out.writeObject(clientObj);
			}
			out.flush();
            log.info("Translation is not successful");
		}
		catch (IOException e) {
			log.warning("Translation is not successful");
		}
	}
	public Bank readBank() {
		int ind = 1;
		Bank bank = new Bank();
		try {
			FileInputStream f = new FileInputStream("bank.ser");
			ObjectInputStream ois = new ObjectInputStream(f);
			Account account;
			Client client;
			do {
				account = (Account)ois.readObject();
				if (account != null) {
					client = (Client)ois.readObject();
					client.setAccount(account);
					bank.addClient(client);
					ind = 0;
				}
			} while (account != null);
			ois.close();
			f.close();
			return bank;
		}
		catch (EOFException e) {
			if (ind == 0)
				return bank;
			else {
				log.info("Deserialization is not successful");
				return null;
			}
		}
		catch (Exception e) {
			log.warning("Deserialization is not successful");
			return null;
		}
	}
}
